package com.example.libraryapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.example.libraryapp.R;
import com.example.libraryapp.fragment.FirstFragment;
import com.example.libraryapp.fragment.FiveFragment;
import com.example.libraryapp.fragment.FourthFragment;
import com.example.libraryapp.fragment.SecondFragment;
import com.example.libraryapp.fragment.ThirdFragment;

/**
 * 首页Activity
 * 
 */
public class MainActivity extends FragmentActivity implements OnClickListener {

	/**
	 * 导航栏Textview
	 */
	private List<TextView> tvList;
	private TextView tvFirst;
	private TextView tvSecond;
	private TextView tvThird;
	private TextView tvFourth;
	private TextView tvFive;
	//Frgment ID
	private List<Integer> idList;
	private static final int ID_FIRST = 1;
	private static final int ID_SECOND = 2;
	private static final int ID_THIRD = 3;
	private static final int ID_FOURTH = 4;
	private static final int ID_FIVE = 5;
	// Fragment名称
	private List<String> tagList;
	private final String TAG_FIRST = FirstFragment.class.getName();
	private final String TAG_SECOND = SecondFragment.class.getName();
	private final String TAG_THIRD = ThirdFragment.class.getName();
	private final String TAG_FOURTH = FourthFragment.class.getName();
	private final String TAG_FIVE = FiveFragment.class.getName();

	private int mainWhich; //当前页标志
	private int lastWhich = -1; // 返回页标志

	private Context mContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		init();
		findViews();
		setListeners();
		showContent();
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.mainWhich = ID_FIRST;
		this.mContext = MainActivity.this;
		tvList = new ArrayList<TextView>();
		idList = new ArrayList<Integer>();
		idList.add(ID_FIRST);
		idList.add(ID_SECOND);
		idList.add(ID_THIRD);
		idList.add(ID_FOURTH);
		idList.add(ID_FIVE);
		tagList = new ArrayList<String>();
		tagList.add(TAG_FIRST);
		tagList.add(TAG_SECOND);
		tagList.add(TAG_THIRD);
		tagList.add(TAG_FOURTH);
		tagList.add(TAG_FIVE);
	}

	/**
	 * 找到控件
	 */
	private void findViews() {
		tvFirst = (TextView) this.findViewById(R.id.first);
		tvSecond = (TextView) this.findViewById(R.id.second);
		tvThird = (TextView) this.findViewById(R.id.third);
		tvFourth = (TextView) this.findViewById(R.id.fourth);
		tvFive = (TextView) this.findViewById(R.id.five);
		tvList.add(tvFirst);
		tvList.add(tvSecond);
		tvList.add(tvThird);
		tvList.add(tvFourth);
		tvList.add(tvFive);
	}

	/**
	 * 设置监听
	 */
	private void setListeners() {
		tvFirst.setOnClickListener(this);
		tvSecond.setOnClickListener(this);
		tvThird.setOnClickListener(this);
		tvFourth.setOnClickListener(this);
		tvFive.setOnClickListener(this);
	}

	/**
	 * 
	 * 监听回调
	 * @param v
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.first: // 
			mainWhich = ID_FIRST;
			showContent();
			break;
		case R.id.second: // 
			mainWhich = ID_SECOND;
			showContent();
			break;
		case R.id.third: // 
			mainWhich = ID_THIRD;
			showContent();
			break;
		case R.id.fourth: // 
			mainWhich = ID_FOURTH;
			showContent();
			break;
		case R.id.five: // 
			mainWhich = ID_FIVE;
			showContent();
			break;
		default:
			break;
		}

	}

	/**
	 * 显示内容
	 * 
	 */
	private void showContent() {
		// TODO Auto-generated method stub
		if (lastWhich == mainWhich) {
			return; 
		}
		lastWhich = mainWhich;
		for (int i = 0; i < idList.size(); i++) {
			if (mainWhich == idList.get(i)) {
				selectedFragment(tagList.get(i));
			} else {
				unSelectedFragment(tagList.get(i));
			}
		}
		// 设置导航TextView样式
		setTextViewSelected(mainWhich);
	}

	/**
	 * 选择Fragment
	 * 
	 * @param tag
	 * @return
	 */
	private Fragment selectedFragment(String tag) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		Fragment fragment = null;
		fragment = fragmentManager.findFragmentByTag(tag);
		if (fragment == null) {
			fragment = Fragment.instantiate(mContext, tag);
			fragmentTransaction.add(R.id.main_content, fragment, tag);

		} else {
			if (fragment.isDetached()) {
				fragmentTransaction.attach(fragment);
			} else {
				fragmentTransaction.show(fragment);
			}
		}

		fragmentTransaction.commitAllowingStateLoss();
		return fragment;
	}

	/**
	 * 隐藏Fragment
	 * 
	 */
	public void unSelectedFragment(String tag) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		Fragment fragment = fragmentManager.findFragmentByTag(tag);
		if (fragment != null) {
			fragmentTransaction.hide(fragment);
		}
		fragmentTransaction.commitAllowingStateLoss();
	}

	/**
	 * 设置TextView选择样式
	 */
	private void setTextViewSelected(int mainWhich) {
		for (int i = 0; i < idList.size(); i++) {
			if (mainWhich == idList.get(i)) {
				tvList.get(i).setSelected(true);
			} else {
				tvList.get(i).setSelected(false);
			}
		}
	}
}
